package com.hxgis.oc.util.fdb;

import com.hxgis.oc.util.ds.SQLiteDataSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.core.JdbcTemplate;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
public class FDB {
    private JdbcTemplate jdbcTemplate;
    List<Long> fileSize = new ArrayList<>();
    private String FileName;

    public FDB(String fileName)
    {
        FileName = fileName.replace(".fdb",".f");
        jdbcTemplate = SQLiteDataSource.createJdbcTemplate(fileName);
        for(int i=0;i<100;i++)
        {
            String fn = FileName + i;
            File file = new File(fn);
            if(!file.exists())
            {
                break;
            }
            fileSize.add(file.length());
        }
    }

    //x,y 20bit,z 8bit
    //[24,24,8]
    long encryptID(long x, long y, long z)
    {
        long id = x << 32 | y << 8 | z;
        return id;
    }

    public byte[] get(String x, String y, String z) {
        byte[] bytes = null;

        if( jdbcTemplate != null)
        {
            String iStr = String.valueOf(encryptID(Long.parseLong(x),Long.parseLong(y),Long.parseLong(z)));
            String sql = "SELECT b,e FROM IDX WHERE i == " + iStr;
            List result =  jdbcTemplate.queryForList(sql);
            if(!result.isEmpty())
            {
                long b,e;
                Map data = (Map) result.get(0);
                Object b1 = data.get("b");
                Object e1 = data.get("e");
                if(b1.getClass() == Integer.class)
                {
                    b = ((Integer) b1).longValue();
                }
                else
                {
                    b = (long)b1;
                }
                if(e1.getClass() == Integer.class)
                {
                    e = ((Integer) e1).longValue();
                }
                else
                {
                    e = (long)e1;
                }
                bytes = getData(b,e);
            }
        }

        return bytes;
    }

    private byte[] getData(long b,long e)
    {
        byte[] bytes = null;

        long sum = 0;
        String fn = "";
        for(int i=0;i<fileSize.size();i++)
        {
            long size = fileSize.get(i);
            sum += size;
            if(b < sum)
            {
                fn = FileName + i;
                sum -= size;
                break;
            }
        }

        File file = new File(fn);
        if(file.exists())
        {
            RandomAccessFile f = null;
            try {
                bytes = new byte[(int)e];
                f = new RandomAccessFile(file,"r");
                f.seek(b - sum);
                f.read(bytes);
            } catch (FileNotFoundException ex) {
                log.error(ex.getMessage());
            } catch (IOException ex) {
                log.error(ex.getMessage());
            } finally {
                if(f != null){
                    try {
                        f.close();
                    } catch (IOException ex) {
                        log.error(ex.getMessage());
                    }
                }
            }
        }

        return bytes;
    }
}
